package com.mcxx.modules.miniSalvation.order.service.read;

import com.mcxx.modules.miniSalvation.order.dao.read.BusiInsureDetailReadDao;
import com.mcxx.modules.miniSalvation.order.dao.write.BusiInsureDetailWriteDao;
import com.mcxx.modules.miniSalvation.order.dto.ExInsuranceDrawUpDTO;
import com.mcxx.modules.miniSalvation.order.param.InsuranceParam;
import com.mcxx.service.DefaultRestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;



@Service
@Slf4j
public class InsuranceInfoJobSerice {


    @Resource
    private BusiInsureDetailReadDao insuranceDrawUpReadDao;

    @Resource
    private BusiInsureDetailWriteDao busiInsureDetailWriteDao;





    @Scheduled(cron = "0 0 0 * * ?")
    public void insuranceInfoJob(){
        Date date = new Date();
        SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date time_date = null;


        try {
            time_date = time.parse(time.format(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }


        InsuranceParam param = new InsuranceParam();
        List<ExInsuranceDrawUpDTO> insuranceInfo = insuranceDrawUpReadDao.getInsuranceOrg(param);
        for (ExInsuranceDrawUpDTO draw : insuranceInfo) {

            if(time_date.compareTo(draw.getStartDate()) == -1){
                draw.setInsureStatus("03");

            }else if(time_date.compareTo(draw.getStartDate()) == 1 && time_date.compareTo(draw.getEndDate()) == -1){
                draw.setInsureStatus("04");

            }else if(time_date.compareTo(draw.getStartDate()) == 1 && time_date.compareTo(draw.getEndDate()) == 1){
                draw.setInsureStatus("05");
                busiInsureDetailWriteDao.delInsuranceDetails(draw.getId());



            }
        }
        int restFlag = busiInsureDetailWriteDao.confirmInit(insuranceInfo);
        log.info("保险状态更新成功，结果是：{}",restFlag);
    }
}
